local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseComponent());

local CommonOHOH = _G.CommonOHOH;
local ConnectClass=CommonOHOH.load.Connect();



function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.ArriveEvent=ConnectClass.new()
    self.GoLostEvent=ConnectClass.new()
end;

function M.dtor(self)
	M.super.dtor(self);
end;

--开始奔跑
function M.Run(self,path)
	
	assert(path,"path==NULL--->")

    self:StopRun()
	self.runing=true

	local  humanoid=self.parent:GetHumanoid()

	local nowPos=self.parent:GetPosition()
    self.path=path
	self.pathId=1
	self.FinishConnect = humanoid.MoveToFinished:Connect(function(reached)
		if reached then
            if self.pathId<#self.path then 
                self.pathId=self.pathId+1
                humanoid:MoveTo(self.path[self.pathId])
            else--到达目的地
                self:StopRun()
                self.ArriveEvent:Action()
            end
			
		else --失败
			if self.FinishConnect then
				self.FinishConnect:Disconnect()
				self.FinishConnect=nil
			end
            self:StopRun()
            self.GoLostEvent:Action(self.path,self.pathId)
		end
	end)

	humanoid:MoveTo(self.path[self.pathId])
	
end

-- 停止奔跑
function M.StopRun(self)
	if self.runing then
		self.runing=false
		if self.FinishConnect then
			self.FinishConnect:Disconnect()
			self.FinishConnect=nil
		end
		local  humanoid=self.parent:GetHumanoid()
		local nowPos=self.parent:GetPosition()
		humanoid:MoveTo(nowPos)
	end
end


return M